package com.ourcraft.informationcollectionsystem.controller.province;

import com.alibaba.fastjson.JSONObject;
import com.ourcraft.informationcollectionsystem.entity.DTO.UserDetailInfo;
import com.ourcraft.informationcollectionsystem.entity.Role;
import com.ourcraft.informationcollectionsystem.service.common.CommonService;
import com.ourcraft.informationcollectionsystem.service.province.userManage.UserManageService;
import com.ourcraft.informationcollectionsystem.utils.authority.AuthorityCode;
import com.ourcraft.informationcollectionsystem.utils.authority.AuthorityValidator;
import com.ourcraft.informationcollectionsystem.utils.response.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 处理用户管理模块相关的请求
 *
 * @author Z0136
 * @since 2023-10-31
 */
@RequestMapping("province/userManage/")
@RestController
@CrossOrigin()
@AuthorityValidator(code = AuthorityCode.USER_MANAGE) // 按照‘用户管理’权限对接口访问进行限制
public class UserManageController {
    @Autowired
    private CommonService commonService;
    @Autowired
    private UserManageService userManageService;

    /**
     * 处理‘province/userManage/allUserInfo’接口的前端请求，获取所有用户信息
     *
     * @param request 用于权限验证
     * @return 所有用户的账号、状态、角色、机构代码、用户名、备案情况、所属市信息
     */
    @GetMapping("allUserInfo")
    public Result<List<UserDetailInfo>> allUserInfo(HttpServletRequest request) {
        return Result.success(userManageService.getAllUserInfo());
    }

    /**
     * 处理‘province/userManage/allRoles’接口的前端请求，获取所有角色信息
     *
     * @param request 用于权限验证
     * @return 所有角色的ID、权限、描述、名称信息
     */
    @GetMapping("allRoles")
    public Result<List<Role>> allRoles(HttpServletRequest request) {
        List<Role> result = commonService.getAllRoles();
        result.forEach(r -> r.setRoleAuthority(null));
        return Result.success(result);
    }

    /**
     * 处理‘province/userManage/createUser’接口的前端请求，创建新角色
     *
     * @param param   包含创建的用户角色和所属市信息
     * @param request 用于权限验证
     * @return 是否创建成功
     */
    @PostMapping("createUser")
    public Result<Boolean> createUser(@RequestBody JSONObject param, HttpServletRequest request) {
        boolean success = userManageService.createUser(param.getInteger("userRole"), param.getString("userCity"));
        if (success) return Result.success();
        return Result.fail();
    }

    /**
     * 处理‘province/userManage/userPassword’接口的前端请求，重置用户密码
     *
     * @param userId  重置密码的用户账号
     * @param request 用于权限验证
     * @return 是否修改成功
     */
    @PutMapping("userPassword")
    public Result<Boolean> resetUserPassword(@RequestParam Integer userId, HttpServletRequest request) {
        boolean success = userManageService.resetUserPassword(userId);
        if (success) return Result.success();
        return Result.fail();
    }

    /**
     * 处理‘province/userManage/userRole’接口的前端请求，修改用户角色
     *
     * @param user    包含被修改用户的用户账号、修改后角色、所属市信息
     * @param request 用于权限验证
     * @return 是否修改成功
     */
    @PutMapping("userRole")
    public Result<Boolean> changeUserRole(@RequestBody UserDetailInfo user, HttpServletRequest request) {
        String message = userManageService.changeUserRole(user);
        if (Objects.equals(message, "success")) return Result.success();
        return Result.fail(message);
    }

    /**
     * 处理‘province/userManage/userStatus’接口的前端请求，修改用户状态
     *
     * @param param   包含被修改用户账号和修改后状态
     * @param request 用于权限验证
     * @return 是否修改成功
     */
    @PutMapping("userStatus")
    public Result<Boolean> changeUserStatus(@RequestBody JSONObject param, HttpServletRequest request) {
        boolean success = userManageService.changeUserStatus(param.getInteger("userAccount"), param.getBoolean("userStatus"));
        if (success) return Result.success();
        return Result.fail();
    }
}
